/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.ColorInt;
import androidx.core.content.ContextCompat;
import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;

public class SelectionView extends FrameLayout {
    private static final String TAG = SelectionView.class.getSimpleName();

    private static final int MENU_BUTTON_SIZE = 5;
    private static final int NORMAL_PALETTE_SIZE = 7;
    private static final int STICKY_NOTE_PALETTE_SIZE = 6;
    private static final float MENU_ROUND_RADIUS = 30;
    private static final float PALETTE_ROUND_RADIUS = 30;
    private static final float PALETTE_POSITION_BIAS = 5;

    private static final int POSITION_TYPE_MENU = 0;
    private static final int POSITION_TYPE_NORMAL_PALETTE = 1;
    private static final int POSITION_TYPE_STICKY_NOTE_PALETTE = 2;

    private RectF box;

    private View menu;
    private View copy;
    private View cut;
    private View delete;
    private View trim;
    private View stickyNote;
    private View normalPalette;
    private View stickyNotePalette;
    private View paletteRed;
    private View paletteBlue;
    private View paletteYellow;
    private View paletteGreen;
    private View palettePurple;
    private View paletteWhite;
    private View paletteBlack;
    private View stickyNotePalettePink;
    private View stickyNotePaletteBlue;
    private View stickyNotePaletteYellow;
    private View stickyNotePaletteGreen;
    private View stickyNotePalettePurple;
    private View stickyNotePaletteWhite;
    private View pushEnd;

    private int imageMenuButtonSize;
    private PointF[] imageMenuPositions;
    private PointF[] menuPositions;
    private PointF[] normalPalettePositions;
    private PointF[] stickyNotePalettePositions;

    private boolean isStickyNoteSelected;

    private Matrix mat;
    private float[] point;

    private float menuOffsetX;
    private float optionMargin;
    private float stickyNoteOffsetX;

    private Listener listener;

    private Display display;
    private Point screenSize;
    private boolean imageOnly; // When selected items are images, hide color the palette.
    private Context context;

    public SelectionView(Context context) {
        super(context);
        init(context);
    }

    public SelectionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SelectionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        mat = new Matrix();
        point = new float[2];
        box = new RectF();

        inflate(context, R.layout.selection_controls, this);

        menu = findViewById(R.id.menu);
        menu.setOnClickListener(v -> {
            menu.setSelected(!menu.isSelected());

            if (menu.isSelected()) {
                copy.setVisibility(View.VISIBLE);
                cut.setVisibility(View.VISIBLE);
                delete.setVisibility(View.VISIBLE);

                if (Props.getPhase() >= Props.Phase.THREE) {
                    showPalette(isStickyNoteSelected);
                    pushEnd.setVisibility(VISIBLE);
                }
            } else {
                copy.setVisibility(View.INVISIBLE);
                cut.setVisibility(View.INVISIBLE);
                delete.setVisibility(View.INVISIBLE);

                if (Props.getPhase() >= Props.Phase.THREE) {
                    hidePalette();
                    pushEnd.setVisibility(INVISIBLE);
                }
            }

            if (listener != null) {
                listener.onMenuClick();
            }
        });

        menuPositions = new PointF[MENU_BUTTON_SIZE];
        for (int i = 0; i < MENU_BUTTON_SIZE; i++) {
            menuPositions[i] = new PointF();
        }
        normalPalettePositions = new PointF[NORMAL_PALETTE_SIZE];
        for (int i = 0; i < NORMAL_PALETTE_SIZE; i++) {
            normalPalettePositions[i] = new PointF();
        }
        stickyNotePalettePositions = new PointF[STICKY_NOTE_PALETTE_SIZE];
        for (int i = 0; i < STICKY_NOTE_PALETTE_SIZE; i++) {
            stickyNotePalettePositions[i] = new PointF();
        }
        imageMenuButtonSize = MENU_BUTTON_SIZE-1;
        imageMenuPositions = new PointF[imageMenuButtonSize];
        for (int i = 0; i < imageMenuButtonSize; i++) {
            imageMenuPositions[i] = new PointF();
        }

        initButtonPositions();

        copy = findViewById(R.id.copy);
        copy.setOnClickListener(actionOnClickListener);
        copy.setVisibility(View.INVISIBLE);

        cut = findViewById(R.id.cut);
        cut.setOnClickListener(actionOnClickListener);
        cut.setVisibility(View.INVISIBLE);

        delete = findViewById(R.id.delete);
        delete.setOnClickListener(actionOnClickListener);
        delete.setVisibility(View.INVISIBLE);

        stickyNote = findViewById(R.id.sticky_note);
        stickyNote.setOnClickListener(actionOnClickListener);

        trim = findViewById(R.id.trim);
        trim.setOnClickListener(actionOnClickListener);

        normalPalette = findViewById(R.id.palette_normal);
        normalPalette.setOnClickListener(actionOnClickListener);
        normalPalette.setVisibility(View.INVISIBLE);
        normalPalette.setScaleX(0.8f);
        normalPalette.setScaleY(0.8f);

        stickyNotePalette = findViewById(R.id.sticky_note_palette_normal);
        stickyNotePalette.setOnClickListener(actionOnClickListener);
        stickyNotePalette.setVisibility(INVISIBLE);
        stickyNotePalette.setScaleX(0.8f);
        stickyNotePalette.setScaleY(0.8f);


        paletteBlack = findViewById(R.id.palette_black);
        paletteBlack.setOnClickListener(actionOnClickListener);
        paletteBlack.setVisibility(View.INVISIBLE);

        paletteBlue = findViewById(R.id.palette_blue);
        paletteBlue.setOnClickListener(actionOnClickListener);
        paletteBlue.setVisibility(View.INVISIBLE);

        paletteGreen = findViewById(R.id.palette_green);
        paletteGreen.setOnClickListener(actionOnClickListener);
        paletteGreen.setVisibility(View.INVISIBLE);

        palettePurple = findViewById(R.id.palette_purple);
        palettePurple.setOnClickListener(actionOnClickListener);
        palettePurple.setVisibility(View.INVISIBLE);

        paletteRed = findViewById(R.id.palette_red);
        paletteRed.setOnClickListener(actionOnClickListener);
        paletteRed.setVisibility(View.INVISIBLE);

        paletteWhite = findViewById(R.id.palette_white);
        paletteWhite.setOnClickListener(actionOnClickListener);
        paletteWhite.setVisibility(View.INVISIBLE);

        paletteYellow = findViewById(R.id.palette_yellow);
        paletteYellow.setOnClickListener(actionOnClickListener);
        paletteYellow.setVisibility(View.INVISIBLE);

        stickyNotePaletteBlue = findViewById(R.id.sticky_note_palette_blue);
        stickyNotePaletteBlue.setOnClickListener(actionOnClickListener);
        stickyNotePaletteBlue.setVisibility(View.INVISIBLE);

        stickyNotePalettePink = findViewById(R.id.sticky_note_palette_pink);
        stickyNotePalettePink.setOnClickListener(actionOnClickListener);
        stickyNotePalettePink.setVisibility(View.INVISIBLE);

        stickyNotePalettePurple = findViewById(R.id.sticky_note_palette_purple);
        stickyNotePalettePurple.setOnClickListener(actionOnClickListener);
        stickyNotePalettePurple.setVisibility(View.INVISIBLE);

        stickyNotePaletteYellow = findViewById(R.id.sticky_note_palette_yellow);
        stickyNotePaletteYellow.setOnClickListener(actionOnClickListener);
        stickyNotePaletteYellow.setVisibility(View.INVISIBLE);

        stickyNotePaletteGreen = findViewById(R.id.sticky_note_palette_green);
        stickyNotePaletteGreen.setOnClickListener(actionOnClickListener);
        stickyNotePaletteGreen.setVisibility(View.INVISIBLE);

        stickyNotePaletteWhite = findViewById(R.id.sticky_note_palette_white);
        stickyNotePaletteWhite.setOnClickListener(actionOnClickListener);
        stickyNotePaletteWhite.setVisibility(View.INVISIBLE);

        pushEnd = findViewById(R.id.push_end);
        pushEnd.setOnClickListener(actionOnClickListener);
        pushEnd.setVisibility(INVISIBLE);

        final Resources res = context.getResources();
        menuOffsetX = res.getDimensionPixelOffset(R.dimen.selection_menu_offset_x);
        optionMargin = res.getDimensionPixelOffset(R.dimen.selection_option_margin);
        stickyNoteOffsetX = res.getDimensionPixelOffset(R.dimen.selection_sticky_note_offset_x);

        display = getDefaultDisplay(context);
        screenSize = new Point();

        imageOnly = false;

        if (Props.getPhase() <= Props.Phase.TWO) {
            stickyNote.setVisibility(View.GONE);
            pushEnd.setVisibility(View.GONE);
            trim.setVisibility(View.GONE);
        }
    }

    private Display getDefaultDisplay(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay();
    }

    private final OnClickListener actionOnClickListener = v -> {
        if (listener != null) {
            switch (v.getId()) {
                case R.id.copy: {
                    listener.onSelectionCopyClick();
                    break;
                }
                case R.id.cut: {
                    listener.onSelectionCutClick();
                    break;
                }
                case R.id.delete: {
                    listener.onSelectionDeleteClick();
                    break;
                }
                case R.id.trim: {
                    trim.setSelected(!trim.isSelected());
                    activeTrimming(trim.isSelected());
                    listener.onTrimingClick();
                    break;
                }
                case R.id.sticky_note: {
                    listener.onSelectionStickyNoteClick();
                    break;
                }
                case R.id.palette_normal: {
                    normalPalette.setSelected(!normalPalette.isSelected());
                    setNormalPaletteVisible(normalPalette.isSelected());
                    break;
                }
                case R.id.sticky_note_palette_normal: {
                    stickyNotePalette.setSelected(!stickyNotePalette.isSelected());
                    setStickynotePaletteVisible(stickyNotePalette.isSelected());
                    break;
                }
                case R.id.palette_black: {
                    listener.onColorChange(ContextCompat.getColor(v.getContext(), R.color.pen_black));
                    break;
                }
                case R.id.palette_blue: {
                    listener.onColorChange(ContextCompat.getColor(v.getContext(), R.color.pen_blue));
                    break;
                }
                case R.id.palette_green: {
                    listener.onColorChange(ContextCompat.getColor(v.getContext(), R.color.pen_green));
                    break;
                }
                case R.id.palette_purple: {
                    listener.onColorChange(ContextCompat.getColor(v.getContext(), R.color.pen_purple));
                    break;
                }
                case R.id.palette_red: {
                    listener.onColorChange(ContextCompat.getColor(v.getContext(), R.color.pen_red));
                    break;
                }
                case R.id.palette_white: {
                    listener.onColorChange(ContextCompat.getColor(v.getContext(), R.color.pen_white));
                    break;
                }
                case R.id.palette_yellow: {
                    listener.onColorChange(ContextCompat.getColor(v.getContext(), R.color.pen_yellow));
                    break;
                }
                case R.id.sticky_note_palette_blue: {
                    listener.onStickynoteColorChange(ContextCompat.getColor(v.getContext(), R.color.sticky_note_blue));
                    break;
                }
                case R.id.sticky_note_palette_green: {
                    listener.onStickynoteColorChange(ContextCompat.getColor(v.getContext(), R.color.sticky_note_green));
                    break;
                }
                case R.id.sticky_note_palette_pink: {
                    listener.onStickynoteColorChange(ContextCompat.getColor(v.getContext(), R.color.sticky_note_pink));
                    break;
                }
                case R.id.sticky_note_palette_purple: {
                    listener.onStickynoteColorChange(ContextCompat.getColor(v.getContext(), R.color.sticky_note_purple));
                    break;
                }
                case R.id.sticky_note_palette_yellow: {
                    listener.onStickynoteColorChange(ContextCompat.getColor(v.getContext(), R.color.sticky_note_yellow));
                    break;
                }
                case R.id.sticky_note_palette_white: {
                    listener.onStickynoteColorChange(ContextCompat.getColor(v.getContext(), R.color.sticky_note_white));
                    break;
                }
                case R.id.push_end: {
                    listener.onPushEndClick();
                    break;
                }
            }
        }
    };


    public void setBoxBounds(float left, float top, float right, float bottom) {
        log("setboxbounds: "+left +"," +top);
        box.set(left, top, right, bottom);
        updateUi();
    }

    private void setNormalPaletteVisible(boolean visible) {
        if (visible) {
            paletteYellow.setVisibility(VISIBLE);
            paletteWhite.setVisibility(VISIBLE);
            paletteRed.setVisibility(VISIBLE);
            palettePurple.setVisibility(VISIBLE);
            paletteGreen.setVisibility(VISIBLE);
            paletteBlue.setVisibility(VISIBLE);
            paletteBlack.setVisibility(VISIBLE);

            copy.setVisibility(INVISIBLE);
            cut.setVisibility(INVISIBLE);
            delete.setVisibility(INVISIBLE);

            if (Props.getPhase() >= Props.Phase.THREE) {
                pushEnd.setVisibility(INVISIBLE);
            }
        } else {
            paletteYellow.setVisibility(INVISIBLE);
            paletteWhite.setVisibility(INVISIBLE);
            paletteRed.setVisibility(INVISIBLE);
            palettePurple.setVisibility(INVISIBLE);
            paletteGreen.setVisibility(INVISIBLE);
            paletteBlue.setVisibility(INVISIBLE);
            paletteBlack.setVisibility(INVISIBLE);

            if (menu.isSelected()) {
                copy.setVisibility(VISIBLE);
                cut.setVisibility(VISIBLE);
                delete.setVisibility(VISIBLE);

                if (Props.getPhase() >= Props.Phase.THREE) {
                    pushEnd.setVisibility(VISIBLE);
                }
            }
        }

        requestLayout();
    }

    private void setStickynotePaletteVisible(boolean visible) {
        if (visible) {
            stickyNotePaletteWhite.setVisibility(VISIBLE);
            stickyNotePaletteGreen.setVisibility(VISIBLE);
            stickyNotePaletteYellow.setVisibility(VISIBLE);
            stickyNotePalettePurple.setVisibility(VISIBLE);
            stickyNotePalettePink.setVisibility(VISIBLE);
            stickyNotePaletteBlue.setVisibility(VISIBLE);

            copy.setVisibility(INVISIBLE);
            cut.setVisibility(INVISIBLE);
            delete.setVisibility(INVISIBLE);

            if (Props.getPhase() >= Props.Phase.THREE) {
                pushEnd.setVisibility(INVISIBLE);
            }
        } else {
            stickyNotePaletteWhite.setVisibility(INVISIBLE);
            stickyNotePaletteGreen.setVisibility(INVISIBLE);
            stickyNotePaletteYellow.setVisibility(INVISIBLE);
            stickyNotePalettePurple.setVisibility(INVISIBLE);
            stickyNotePalettePink.setVisibility(INVISIBLE);
            stickyNotePaletteBlue.setVisibility(INVISIBLE);

            if (menu.isSelected()) {
                copy.setVisibility(VISIBLE);
                cut.setVisibility(VISIBLE);
                delete.setVisibility(VISIBLE);

                if (Props.getPhase() >= Props.Phase.THREE) {
                    pushEnd.setVisibility(VISIBLE);
                }
            }
        }

        requestLayout();
    }


    /**
     * Shows selection controls.
     */
    public void show(boolean isStickyNoteSelected) {
        setVisibility(View.VISIBLE);
        this.isStickyNoteSelected = isStickyNoteSelected;
    }


    public void show(int mode) {
        setVisibility(VISIBLE);
        switch (mode) {
            case 0: {
                this.isStickyNoteSelected = false;
                if (Props.getPhase() > Props.Phase.TWO) {
                    trim.setVisibility(INVISIBLE);
                }
                break;
            }
            case 1: {
                this.isStickyNoteSelected = true;
                if (Props.getPhase() > Props.Phase.TWO) {
                    trim.setVisibility(INVISIBLE);
                }
                break;
            }
            case 2: {
                this.isStickyNoteSelected = false;
                if (Props.getPhase() > Props.Phase.TWO) {
                    trim.setVisibility(VISIBLE);
                }
                break;
            }
        }
    }

    /**
     * Hides selection controls.
     */
    public void hide() {
        setVisibility(View.GONE);
        trim.setSelected(false);
        activeTrimming(false);

        // reset menu
        if (menu.isSelected()) {
            menu.callOnClick();
        }
    }

    public void activeTrimming(boolean t) {
        stickyNotePalette.setSelected(false);
        normalPalette.setSelected(false);
        setStickynotePaletteVisible(false);
        setNormalPaletteVisible(false);
        if (t) {
            menu.setVisibility(INVISIBLE);

            if (Props.getPhase() >= Props.Phase.THREE) {
                stickyNote.setVisibility(INVISIBLE);
            }
        } else {
            menu.setVisibility(VISIBLE);

            if (Props.getPhase() >= Props.Phase.THREE) {
                stickyNote.setVisibility(VISIBLE);
            }
        }

    }

    public void showPalette(boolean isStickynoteSelected) {
        if (isStickynoteSelected) {
            normalPalette.setVisibility(View.INVISIBLE);
//            stickyNotePalette.setVisibility(View.VISIBLE);
            stickyNotePalette.setVisibility(imageOnly ? View.GONE : View.VISIBLE);
        } else {
//            normalPalette.setVisibility(View.VISIBLE);
            normalPalette.setVisibility(imageOnly ? View.GONE : View.VISIBLE);
            stickyNotePalette.setVisibility(View.INVISIBLE);
        }

        requestLayout();
    }

    public void hidePalette() {
        normalPalette.setVisibility(View.INVISIBLE);
        paletteYellow.setVisibility(INVISIBLE);
        paletteWhite.setVisibility(INVISIBLE);
        paletteRed.setVisibility(INVISIBLE);
        palettePurple.setVisibility(INVISIBLE);
        paletteGreen.setVisibility(INVISIBLE);
        paletteBlue.setVisibility(INVISIBLE);
        paletteBlack.setVisibility(INVISIBLE);
        normalPalette.setSelected(false);

        stickyNotePalette.setVisibility(View.INVISIBLE);
        stickyNotePaletteWhite.setVisibility(INVISIBLE);
        stickyNotePaletteGreen.setVisibility(INVISIBLE);
        stickyNotePaletteYellow.setVisibility(INVISIBLE);
        stickyNotePalettePurple.setVisibility(INVISIBLE);
        stickyNotePalettePink.setVisibility(INVISIBLE);
        stickyNotePaletteBlue.setVisibility(INVISIBLE);
        stickyNotePalette.setSelected(false);
    }

    /**
     * Generates points around a circle.
     *
     * @param points output, an array of points
     * @param pointSize number of points to generate
     * @param startDegree starting angle, in degrees
     * @param endDegree ending angle, in degrees
     * @param type a <code>POSITION_TYPE_<em>VALUE</em></code> constant
     */
    private void generatePointPosition(PointF[] points, int pointSize, float startDegree, float endDegree, int type) {
        float degreeDiff = (endDegree - startDegree) / (pointSize - 1);

        point[0] = 0;
        switch (type) {
            case POSITION_TYPE_MENU: {
                point[1] = 0 + MENU_ROUND_RADIUS;
                break;
            }
            case POSITION_TYPE_NORMAL_PALETTE:
            case POSITION_TYPE_STICKY_NOTE_PALETTE: {
                point[1] = 0 + PALETTE_ROUND_RADIUS;
                break;
            }
        }

        if (startDegree != 0) {
            mat.setRotate(-startDegree);
            mat.mapPoints(point);
        }

        mat.setRotate(-degreeDiff);
        for (int i = 0; i < pointSize; i++) {
            if (type == POSITION_TYPE_MENU) {
                points[i].set(point[0], point[1]);
            } else {
                points[i].set(point[0] + PALETTE_POSITION_BIAS, point[1] + PALETTE_POSITION_BIAS);
            }

            mat.mapPoints(point);
        }
    }

    private void initButtonPositions() {
        // type is for different radius\start\end angle
        generatePointPosition(menuPositions, MENU_BUTTON_SIZE, 0, 288, POSITION_TYPE_MENU);
        generatePointPosition(normalPalettePositions, NORMAL_PALETTE_SIZE, 0, 270, POSITION_TYPE_NORMAL_PALETTE);
        generatePointPosition(stickyNotePalettePositions, STICKY_NOTE_PALETTE_SIZE, 10, 260, POSITION_TYPE_STICKY_NOTE_PALETTE);
        generatePointPosition(imageMenuPositions, imageMenuButtonSize, 0, 270, POSITION_TYPE_MENU);
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    private void updateUi() {

        float menuHeight = menu.getHeight();
        if(menuHeight == 0)
            menuHeight = context.getResources().getDimensionPixelSize(R.dimen.menu_button_height);
//        log("update ui option margin:"+optionMargin +"menuheight:"+menuHeight);
//        if (Props.getPhase() <= Props.Phase.TWO) {
            menu.setX(box.right + menuOffsetX);
            menu.setY(box.top - menuHeight / 2.0f);

            adjustMenuPosition();

            copy.setX(menu.getX());
            copy.setY(menu.getY() + menuHeight + optionMargin);

            cut.setX(copy.getX());
            cut.setY(copy.getY() + menuHeight + optionMargin);

            delete.setX(cut.getX());
            delete.setY(cut.getY() + menuHeight + optionMargin);

//            stickyNote.setX(box.left - stickyNote.getWidth() + stickyNoteOffsetX);
//            stickyNote.setY(box.top - stickyNote.getHeight() / 2.0f);
//
//            trim.setX(box.left + stickyNoteOffsetX - trim.getWidth());
//            trim.setY(box.bottom - trim.getHeight() / 2.0f);
//
//        } else {
//            menu.setX(box.right + menuOffsetX);
//            menu.setY(box.top - menu.getHeight() / 2.0f);
//
//            adjustMenuPosition();
//
//            if (imageOnly) {
//                stickyNotePalette.setVisibility(GONE);
//                normalPalette.setVisibility(GONE);
//
//                generatePointPosition(menuPositions, MENU_BUTTON_SIZE, 0, 288, POSITION_TYPE_MENU);
//
//                cut.setX(menu.getX() + menuPositions[0].x);
//                cut.setY(menu.getY() + menuPositions[0].y);
//
//                copy.setX(menu.getX() + menuPositions[1].x);
//                copy.setY(menu.getY() + menuPositions[1].y);
//
////                stickyNotePalette.setX(menu.getX() + menuPositions[2].x);
////                stickyNotePalette.setY(menu.getY() + menuPositions[2].y);
//
////                normalPalette.setX(menu.getX() + menuPositions[2].x);
////                normalPalette.setY(menu.getY() + menuPositions[2].y);
//
//                pushEnd.setX(menu.getX() + menuPositions[2].x);
//                pushEnd.setY(menu.getY() + menuPositions[2].y);
//
//                delete.setX(menu.getX() + menuPositions[3].x);
//                delete.setY(menu.getY() + menuPositions[3].y);
//
//                requestLayout();
//
//            } else {
//                generatePointPosition(menuPositions, MENU_BUTTON_SIZE, 0, 288, POSITION_TYPE_MENU);
//
//                cut.setX(menu.getX() + menuPositions[0].x);
//                cut.setY(menu.getY() + menuPositions[0].y);
//
//                copy.setX(menu.getX() + menuPositions[1].x);
//                copy.setY(menu.getY() + menuPositions[1].y);
//
//                stickyNotePalette.setX(menu.getX() + menuPositions[2].x);
//                stickyNotePalette.setY(menu.getY() + menuPositions[2].y);
//
//                normalPalette.setX(menu.getX() + menuPositions[2].x);
//                normalPalette.setY(menu.getY() + menuPositions[2].y);
//
//                pushEnd.setX(menu.getX() + menuPositions[3].x);
//                pushEnd.setY(menu.getY() + menuPositions[3].y);
//
//                delete.setX(menu.getX() + menuPositions[4].x);
//                delete.setY(menu.getY() + menuPositions[4].y);
//            }
//
//            stickyNote.setX(box.left - stickyNote.getWidth() + stickyNoteOffsetX);
//            stickyNote.setY(box.top - stickyNote.getHeight() / 2.0f);
//
//            trim.setX(box.left + stickyNoteOffsetX - trim.getWidth());
//            trim.setY(box.bottom - trim.getHeight() / 2.0f);
//
//            palettePurple.setX(normalPalette.getX() + normalPalettePositions[0].x);
//            palettePurple.setY(normalPalette.getY() + normalPalettePositions[0].y);
//
//            paletteBlue.setX(normalPalette.getX() + normalPalettePositions[1].x);
//            paletteBlue.setY(normalPalette.getY() + normalPalettePositions[1].y);
//
//            paletteGreen.setX(normalPalette.getX() + normalPalettePositions[2].x);
//            paletteGreen.setY(normalPalette.getY() + normalPalettePositions[2].y);
//
//            paletteWhite.setX(normalPalette.getX() + normalPalettePositions[3].x);
//            paletteWhite.setY(normalPalette.getY() + normalPalettePositions[3].y);
//
//            paletteRed.setX(normalPalette.getX() + normalPalettePositions[4].x);
//            paletteRed.setY(normalPalette.getY() + normalPalettePositions[4].y);
//
//            paletteYellow.setX(normalPalette.getX() + normalPalettePositions[5].x);
//            paletteYellow.setY(normalPalette.getY() + normalPalettePositions[5].y);
//
//            paletteBlack.setX(normalPalette.getX() + normalPalettePositions[6].x);
//            paletteBlack.setY(normalPalette.getY() + normalPalettePositions[6].y);
//
//            stickyNotePalettePurple.setX(stickyNotePalette.getX() + stickyNotePalettePositions[0].x);
//            stickyNotePalettePurple.setY(stickyNotePalette.getY() + stickyNotePalettePositions[0].y);
//
//            stickyNotePaletteYellow.setX(stickyNotePalette.getX() + stickyNotePalettePositions[1].x);
//            stickyNotePaletteYellow.setY(stickyNotePalette.getY() + stickyNotePalettePositions[1].y);
//
//            stickyNotePaletteWhite.setX(stickyNotePalette.getX() + stickyNotePalettePositions[2].x);
//            stickyNotePaletteWhite.setY(stickyNotePalette.getY() + stickyNotePalettePositions[2].y);
//
//            stickyNotePalettePink.setX(stickyNotePalette.getX() + stickyNotePalettePositions[3].x);
//            stickyNotePalettePink.setY(stickyNotePalette.getY() + stickyNotePalettePositions[3].y);
//
//            stickyNotePaletteGreen.setX(stickyNotePalette.getX() + stickyNotePalettePositions[4].x);
//            stickyNotePaletteGreen.setY(stickyNotePalette.getY() + stickyNotePalettePositions[4].y);
//
//            stickyNotePaletteBlue.setX(stickyNotePalette.getX() + stickyNotePalettePositions[5].x);
//            stickyNotePaletteBlue.setY(stickyNotePalette.getY() + stickyNotePalettePositions[5].y);
//        }

    }

    private void adjustMenuPosition() {
        if (display != null) {
            display.getSize(screenSize);
            if(Props.isFake4k()){
                screenSize.x *= 2;
                screenSize.y *= 2;
            }

            if ((menu.getX() + menu.getWidth()) > screenSize.x && box.left < screenSize.x) {
                menu.setX(screenSize.x - menu.getWidth() - menuOffsetX);
            }

            if ((menu.getY()) < 0 && box.bottom > 0) {
                menu.setY(0 + menuOffsetX);
            }
        }
    }

    public void setImageOnly(boolean imageOnly) {
        this.imageOnly = imageOnly;
        updateUi();
    }

    public interface Listener {
        void onSelectionCopyClick();

        void onSelectionCutClick();

        void onSelectionDeleteClick();

        void onSelectionStickyNoteClick();

        void onColorChange(@ColorInt int color);

        void onStickynoteColorChange(@ColorInt int color);

        void onPushEndClick();

        void onTrimingClick();

        void onMenuClick();
    }

    private static void log(String msg) {
        Log.d(TAG, msg);
    }
}
